var SiteImHandler = function() {
	var self = this;
	this.join_group_count = 0;

	this.onopen = function(event, ws) {
		// siteim.joinGroup();
	};

	/**
   * 收到服务器发来的消息
   * @param {*} event 
   * @param {*} ws 
   */
	this.onmessage = function(event, ws) {//实例方法
		var arrayBuffer = event.data;

		// log('receive data: ', arrayBuffer, ws)
		var uint8array = null;
		var firstbyte = new Uint8Array(arrayBuffer, 0, 2);
		console.log("firstbyte---------->"+firstbyte);
		var firstchar = new TextDecoder('utf-8').decode(firstbyte);
        console.log("firstchat----------->"+firstchar);
		var isZipped = false;
		var isZippedStr = '';
		if (firstchar.indexOf('x') != -1){
			// 压缩过的
			isZipped = true;
			isZippedStr = '(zipped)';
			var zipedUint8array = new Uint8Array(arrayBuffer, 2);
            console.log("zipedUint8array---------->"+zipedUint8array);
			uint8array = pako.ungzip(zipedUint8array);
            console.log("uint8array---------->"+uint8array);
		} else {
			uint8array = new Uint8Array(arrayBuffer);
            console.log("uint8arrayelse---------->"+uint8array);
		}

		var data = new TextDecoder('utf-8').decode(uint8array);
		log('receive data' + isZippedStr + ': ' + data)
        console.log("data---------->"+data);
		if (!data || data.length < siteim_command_len) {
			error('data wrong' + isZippedStr + ', the data length must be >= ' + siteim_command_len, data);
			return;
		}

		var commandstr = data.substr(0, siteim_command_len);
        console.log("commandstr---------->"+commandstr);
		var commandName = SiteImCommandReverse[commandstr];
        console.log("SiteImCommandReverser---------->"+JSON.stringify(SiteImCommandReverse));
		if (!commandName) {
			error('commandstr is ' + commandstr + isZippedStr + ', but con not find commandName');
			return;
		}

		var siteImBsHandler = SiteImBsHandler[commandName];
		if (!siteImBsHandler) {
			error('can not found siteImBsHandler, commandName is ' + commandName + isZippedStr);
			return;
		}

		if (data.length > siteim_command_len) {
			var bodyStr = data.substr(siteim_command_len);

			log('received:' + commandName + isZippedStr + '\r\n, body string is :' + bodyStr);
			var bodyObj = null;
			try {
				bodyObj = JSON.parse(bodyStr);
			} catch (error) {
				error(
					'can not parse to object, commandName is ' +
						commandName +
						isZippedStr +
						', body string is ' +
						bodyStr
				);
				return;
			}

			// log("received " + commandName + ", body string is :" + bodyStr);
			siteImBsHandler.call(siteImBsHandler, ws, event, commandName, bodyStr, bodyObj);
		} else {
			log('received:' + commandName + isZippedStr + ', body string is null');

			siteImBsHandler.call(siteImBsHandler, ws, event, commandName, null, null);
		}

		//
	};

	this.onclose = function(e, ws) {
		// error(e, ws)
	};

	this.onerror = function(e, ws) {
		// error(e, ws)
	};

	/**
   * 发送心跳，本框架会自动定时调用该方法，请在该方法中发送心跳
   * @param {*} ws 
   */
	this.ping = function(ws) {
		// log("发心跳了")
		// ws.send('心跳内容')

		// siteim.send()

		siteim.send(SiteImCommand.HEARTBEAT_REQ, null);
	};
};
